/*
 * Sensor.cc
 *
 *  Created on: Sep 22, 2012
 *      Author: HUNG
 */

#ifndef __TARGETTRACKING_SENSOR_H_
#define __TARGETTRACKING_SENSOR_H_

#include <omnetpp.h>
#include <string.h>
#include "RoutingDef.h"
#include "Message_m.h"
#include "Bastation.h"
#include "GaussianNoise.h"
#include "Matrix.h"
#include "Kalman.h"
#include <time.h>

class Sensor : public cSimpleModule
{
    public:
        int myID;
        int xpos, ypos;
        double trRange;
        double ssRange;
        double energy;
        double infoTarget;          //this is for simulation = 1/R_true
        double R_measurement;                     //distance measurement between sensor and target
        Sensor *trNeighbor[NUMBERNODES];
        Sensor *clusterHead;
        Sensor *relayNode;
        Bastation *bs;
        cModule *tar1;
        Sensor *memberCluster[NUMBERNODES];
        GaussianNoise noise;
        int LastModuleId;
        int numNeighbor;
        int frameNumber;
        bool isClusterHead;
        bool isDead;
        bool isRelayNode;
        bool active;    //true: is actived
        bool hasPath;
        bool hasLinks;
        int totalEventModule;
        int totalStatusInfoMsg;
        int numDataToCH;
        bool hasUseFile;
        int typeProtocol;
        int typeEstimate;
        double sigmaMeasurementNoise;
        int countPackageTransmit;
        NotifyDetectTarget *detectSeflMsg;

        //KF
        bool hasOldInfoTarget;
        Kalman KF;
        double timeStep;
        double processNoiseIntensity;
        Matrix X;
        Matrix P;
        ScheduleReset *scheduleResetMsg;

        //EMRP
        double switchLevel;
        Sensor *backupNode;
        double relayEnergy;
        double backupEnergy;
        double relayEnergyEstimate;

        //CSMA var
        int BE; //back off exponent
        int NB; //number of times Backoff
        int flag; //kiem tra moi truong
        int macMinBE;
        int macMaxCSMABackoffs;
        int aMaxBE;
        double CCAduration;
        double symbol;
        double aUnitBackoffPeriod;
        double aMinLIFSPeriod;
        double aMinSIFSPeriod;
        double aTurnAroundTime;
        double bitRate;
        cQueue CSMAQueueMsg;
        cQueue QueueMsg;
        SensorMessage *msgStore;
        int idError[NUMBERNODES];
        int countIdError;
        TimeOut *timeoutREQ_Relay;
        TimeOut *timeoutREQ_CH;
        double timeOutReceiveAck;

        virtual void initialize();
        virtual void handleMessage(cMessage *msg);
        void arrangeNodes();
        void setDisplay();
        void setDisplay(const char *color);
        void findLinks();                           //initial links between sensors
        void createLinks(double range);
        void createLink(Sensor *s);
        void deleteLink(Sensor *s);
        void sendEventNotifyMessage(int totalSensorInCluster);
        void sendTDMA();
        double distanceCal(double x, double y);
        void electCH();
        void locateTarget();
        void transmitDataToBS();
        void findRelayNode();
        void sendRelayRequest();
        Sensor* getSensor(int id);
        void energySend(int bits, double d);
        void energyReceive(int bits);
        void resetDisplay();
        void sendSeflMsgToCH();

        //KF
        void estimatesTarget();
        Matrix getCovarianceMatrix(double covarianceArray[]);
        void broadcastTargetInfo(double distance);

        //EMRP
        void broadcast();
        void scheduleAtInit();
        void findRelayBackupNode();
        void estimateEnergyRelay();

        //CSMA
        void backOff();
        void initCSMA(Package *package);
        void clearChannelAssessment();
        void chiemKenh(double distance);
        void giaiPhongKenh(double distance);
        void storeIdError(int idPrevMsg, int idCurrMsg);  //chi cho mo phong
        bool checkMsgError(int idSender);               //chi cho mo phong

};


#endif
